module executor.friend.DealApplyContactExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import client.GateServerClient;
import client.MsgServerClient;
import service.ContactsService;
import service.ApplyService;
import repository.FriendRepository;
import core.stdc.time;

@TransportServer(LogicServerName)
class DealApplyContactExecutor : AbstractExecutor!(DealApplyContactExecutor){

    //处理好友申请接口
    @MessageId(MESSAGE.CMD_DEAL_FRIEND_REQ)
    void dealApplyContact(TransportContext ctx, MessageBuffer buffer) {
        auto req = new DealApplyContact();
        buffer.data.fromProtobuf!DealApplyContact(req);
        TransportSession msgClient = null;
        auto friendRep = new FriendRepository;

        if (req.succeeded) //同意添加好友
        {
            User[] users = ApplyService.getUsers([req.fromId,req.toId],req.fromId);
            auto addFriend = new AddContact;
            addFriend.chatId = req.chatId;
            if (users !is null || users.length == 2)
            {
                if(!ApplyService.putFriend(req.chatId,req.fromId,req.toId,cast(int)req.src)) //已经是好友了
                {
                    logError("already friend!!!");
                    // return;
                }

                /**
                    向好友双方发送回调消息
                **/
                msgClient =  MsgServerClient.getInstance().getForwardClient(to!int(users[1].id));
                addFriend.user =  users[0];
                addFriend.toId =  users[1].id;
                msgClient.send(new MessageBuffer(MESSAGE.CMD_DEAL_FRIEND_RES,addFriend.toProtobuf.array));

                msgClient =  MsgServerClient.getInstance().getForwardClient(to!int(users[0].id));
                addFriend.user =  users[1];
                addFriend.toId =  users[0].id;
                msgClient.send(new MessageBuffer(MESSAGE.CMD_DEAL_FRIEND_RES,addFriend.toProtobuf.array));
            }

            ChatMapAction chatMapAction = new ChatMapAction;
            chatMapAction.chatId = req.chatId;
            chatMapAction.type = ChatMapActionType.CREATE;
            chatMapAction.manager = req.fromId;
            chatMapAction.timestamp = core.stdc.time.time(null);
            foreach(User u ; users)
            {
                chatMapAction.member ~= u.id;
            }
            msgClient =  MsgServerClient.getInstance().getForwardClient(to!int(req.chatId));
            msgClient.send(new MessageBuffer(MESSAGE.CMD_CHAT_MAP_ACTION, chatMapAction.toProtobuf.array));//向msgserver发送chatId对应user列表
        }

        ApplyService.updateApplyContactReq(req.chatId, req.succeeded ? 1: 2); //更新表状态同意or忽略

        // Notification notice = new Notification();
        // notice.fromId = req.fromId;
        // notice.toId = req.toId;
        // notice.type = req.succeeded ? NotificationType.AGREE_APPLY_FRIEND:NotificationType.DISAGREE_APPLY_FRIEND;

        // msgClient =  MsgServerClient.getInstance().getForwardClient(to!int(req.fromId));
        // notice.destination = req.fromId;
        // msgClient.send(new MessageBuffer(MESSAGE.CMD_NOTIFY_TRANSFER,notice.toProtobuf.array));
        // msgClient =  MsgServerClient.getInstance().getForwardClient(to!int(req.toId));
        // notice.destination = req.toId;
        // msgClient.send(new MessageBuffer(MESSAGE.CMD_NOTIFY_TRANSFER,notice.toProtobuf.array));
    }
}
